import java.util.*;

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val=val;
    }
}

public class BinaryTree {

    public static int i=0;
    public TreeNode createTree(String str) {
        TreeNode root=null;
        if(str.charAt(i)!='#') {
            root=new TreeNode(str.charAt(i));
            i++;
            root.left=createTree(str);
            root.right=createTree(str);
        }else {
            i++;
        }
        return root;
    }

//    层序遍历（无返回值版）
    public void levelOrder(TreeNode root) {
        if(root==null)return;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null)queue.offer(cur.left);
            if(cur.right!=null)queue.offer(cur.right);
        }
        System.out.println();
    }

//    层序遍历（返回二维数组版）
    /*


    public List<List<Integer>> levelOrder2(TreeNode root) {
        if(root==null) return null;
        List<List<Integer>> ret=new ArrayList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size=queue.size();
            List<Integer> list=new ArrayList<>();
            while(size>0) {
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
                size--;
            }
            ret.add(list);
        }
        return ret;
    }*/

//    判断一棵树是否是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root==null)  return true;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur=queue.poll();
            if(cur==null) {
                break;
            }else {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
        }
        while(!queue.isEmpty()) {
            TreeNode node=queue.peek();
            if(node!=null) return false;
            else queue.poll();
        }
        return true;
    }

//    求最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        if(root==p || root==q) return root;

        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);

        if(leftTree!=null && rightTree!=null) return root;
        else if(leftTree!=null) return leftTree;
        else return rightTree;
    }

//    求最近公共祖先（链表思路）
    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;

        Stack<TreeNode> stackP=new Stack<>();
        getPath(root,p,stackP);
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,q,stackQ);

        int sizeP=stackP.size();
        int sizeQ=stackQ.size();

        if(sizeP>sizeQ) {
            int size=sizeP-sizeQ;
            while(size!=0) {
                stackP.pop();
                size--;
            }
        }else {
            int size=sizeQ-sizeP;
            while(size!=0) {
                stackQ.pop();
                size--;
            }
        }
        while(!stackP.isEmpty() && !stackQ.isEmpty()) {
            if(stackP.peek() == stackQ.peek()) {
                return stackP.peek();
            }else {
                stackP.pop();
                stackQ.pop();
            }
        }
        return null;
    }


    //如何确定节点的路径是否正确？
    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if(root==null) return false;
        stack.push(root);
        if(root==node) return true;

        boolean flg=getPath(root.left,node,stack);
        if(flg) return true;

        boolean flg2=getPath(root.left,node,stack);
        if(flg2) return true;

        stack.pop();
        return false;
    }

}
